package com.zy.ems.common.page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * 包含“分页”信息的列表。
 * 
 * <p>要得到总页数请使用 toPaginator().getTotalPages();</p>
 * 
 * @author Luffy
 */
public class PaginationResult<T> implements Serializable {
    private static final long serialVersionUID = 5617803310607216956L;

    /**
     * 初始的每页记录数
     */
    public final static int DEFAULT_PAGE_SIZE = 10;

    /**
     * 查询返回的最多页数
     */
    public final static int MAX_PAGENUM = 100;

    /**
     * 查询返回的最多记录数
     */
    public final static int MAX_ITEMNUM = 5000;

    /**
     * 分页器
     */
    private Paginator paginator;

    /**
     * 总记录数
     */
    @JsonProperty(value = "totalItemNum")
    private int totalNum;

    /**
     * 存放当前页的实体列表
     */
    @JsonProperty(value = "items")
    private List<T> curPageItems = new ArrayList<T>();

    public PaginationResult(Collection<? extends T> c) {
        this.totalNum = c.size();
        this.curPageItems = new ArrayList<T>(c);
    }

    public PaginationResult(Paginator paginator) {
        this.totalNum = paginator.getTotalNum();
        this.paginator = paginator;
    }

    public PaginationResult(int page, int pageSize, int totalNum) {
        this(new Paginator(page, pageSize, totalNum));
    }

    public PaginationResult(Collection<? extends T> c, Paginator paginator) {
        this.curPageItems = new ArrayList<T>(c);
        this.paginator = paginator;
        this.totalNum = paginator.getTotalNum();
    }

    public PaginationResult(Collection<? extends T> c, int page, int pageSize,
            int totalNum) {
        this(c, new Paginator(page, pageSize, totalNum));
    }

    @JsonProperty(value = "pageSize")
    public int getPageSize() {
        return this.paginator.getPageSize();
    }

    @JsonProperty(value = "page")
    public int getPage() {
        return this.paginator.getPage();
    }

    /**
     * 获取分页器，通过Paginator可以得到总页数等值。
     * 
     * @return
     */
    public Paginator getPaginator() {
        return this.paginator;
    }

    public int getTotalNum() {
        return totalNum;
    }

    public List<T> getCurPageItems() {
        return curPageItems;
    }

    public void setCurPageItems(List<T> curPageItems) {
        this.curPageItems = curPageItems;
    }
}
